import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import {FormValidatorService} from 'great-ngform';

@Component({
  selector: 'app-string',
  templateUrl: './string.component.html',
  styleUrls: ['./string.component.less']
})
export class StringComponent implements OnInit {

  form: FormGroup;

  constructor(
    private fb: FormBuilder,
    private formValidatorService: FormValidatorService
  ) {
  }

  // tslint:disable-next-line:typedef
  ngOnInit() {
    this.form = this.fb.group({
      strLength: new FormControl(null, [
        this.formValidatorService.strLength({
          maxLength: 10,
        })
      ]),
      strLength1: new FormControl(null, [
        this.formValidatorService.strLength({
          length: 10
        })
      ]),
      strLength2: new FormControl(null, [
        this.formValidatorService.strLength({
          minLength: 10
        })
      ]),
      strLength3: new FormControl(null, [
        this.formValidatorService.strLength({
          minLength: 5,
          maxLength: 10
        })
      ]),
      strLength4: new FormControl(null, [
        this.formValidatorService.strLength({
          minLength: 5,
          errorMsg: '最少输入5个字节！',
        }),
        this.formValidatorService.strLength({
          maxLength: 10,
          errorMsg: '最多输入10个字节！'
        }),
        this.formValidatorService.required({
          errorMsg: '必填项不能为空！'
        })
      ]),
    });
  }

  submitForm() {
    if (this.form.valid) {

    } else {
      this.formValidatorService.checkForm(this.form);
    }
  }

}
